package com.example.qingcaizqc.lesson22logintoremoteserver;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;


public class MainActivity extends Activity {

    public Button mButtonLogin;
    public EditText mUserName;
    public EditText mPassword;
    public TextView mResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mButtonLogin = (Button)findViewById(R.id.button_login);
        mButtonLogin.setOnClickListener(new LoginListener());
        mUserName = (EditText)findViewById(R.id.editText_username);
        mPassword = (EditText)findViewById(R.id.editText_pwd);
        mResult = (TextView)findViewById(R.id.textView_result);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public class LoginListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            String un = mUserName.getText().toString();
            String pwd = mPassword.getText().toString();
            new LoginTask().execute(new String[]{un, pwd});
        }
    }

    public class LoginTask extends AsyncTask<String, Integer, String>{

        @Override
        protected String doInBackground(String... userinfo) {
            System.out.println(userinfo[0] + ". " + userinfo[1]);
            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("username", userinfo[0]));
            postParameters.add(new BasicNameValuePair("password", userinfo[1]));
            String response = null;
            System.out.println("main thread request   begin.");
            try {
                response = SimpleHttpClient.executeHttpPost("http://10.32.168.138:8080/Android-Lesson-22-loginRemoteServer/login.do", postParameters);
                System.out.print("main thread request.");
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(response == null){
                return "error";
            }else {
                System.out.println( "get response");
                String res = response.toString();
                String resp = res.replaceAll("\\s+", "");

                System.out.println("result = " + resp);
                return resp;
            }
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            mResult.setText(result);
        }
    }
}
